package cn.jiyun.mongodb;

/**
 * @author Fay
 * @date 2022/6/29&16:56
 */

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.junit.Test;

/**
 * 查询评论大于1000的记录
 */
public class mongodbDemo2 {
   @Test
    public void find(){
       //建立连接
       MongoClient client = new MongoClient("127.0.0.1");
        //获取数据库
       MongoDatabase spitdb = client.getDatabase("spitdb");
        //获取集合
       MongoCollection<Document> spit = spitdb.getCollection("spit");

       BasicDBObject bson = new BasicDBObject("visits",new BasicDBObject("$gt",1000));

       FindIterable<Document> documents = spit.find(bson);
       for (Document document : documents) {
           System.out.println("内容："+	document.getString("content"));
           System.out.println("用户ID:"+document.getString("userid"));
           System.out.println("浏览量："+document.getInteger("visits"));
           System.out.println("=========================");
       }
       client.close();
   }
}

